// pages/balance/balance.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollViewHeigth: 300,
    tabSelected: 0,

    pages: 0,
    balanceList: [],
    showRechange: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let showRechange = false;
    let pluginList = wx.getStorageSync('pluginList');
    for(let i=0;i<pluginList.length;i++){
      if(pluginList[i].auth_id===176 && pluginList[i].personal==='true'){
        showRechange = true;
      }
    }
    options.pageName && (wx.setNavigationBarTitle({
      title: options.pageName,
    }))
    
    this.setData({
      tabSelected: 0,
      showRechange: showRechange
    })

    var that = this;
    var query = wx.createSelectorQuery();
    //选择id
    query.select('#head').boundingClientRect()
    query.select('#title').boundingClientRect()
    query.exec(function(res) {
      console.log(res[0].height, res[1].height)
      that.setData({
        scrollViewHeigth: wx.getSystemInfoSync().windowHeight - res[0].height+10,
      })
    })
  },

  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      tabSelected: e.detail.current,
      pages: 0,
      balanceList: []
    })
    this.getWalletRecord();
  },

  //点击切换
  clickTab: function(e) {
    if (e.currentTarget.dataset.idx != this.data.tabSelected) {
      this.setData({
        tabSelected: e.currentTarget.dataset.idx,
        pages: 0,
        balanceList: []
      })
      // this.getWalletRecord();
    }
  },

  getUserInfo: function () {
    var that = this;
    var data = {
      point_url: app.server.agentApi.getMemberDetail
    };
    app.server.getConnect(data).then(function (res) {
      that.setData({
        userInfo: res.data,
      })
    })
  },

  getWalletRecord: function() {
    var that = this;
    this.data.pages++;
    var data = {
      point_url: app.server.agentApi.getWalletRecord,
      openid: app.globalData.openid,
      type: this.data.tabSelected === 0 ? 'wallet' : 'donation',
      pageNumber: this.data.pages,
      pageSize: 5,
    };
    app.server.getConnect(data).then(function(res) {
      console.log(res);
      that.setData({
        balanceList: that.data.balanceList.concat(res.data.rows)
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      pages: 0,
      balanceList: []
    })
    this.getWalletRecord();
    this.getUserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getWalletRecord();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
